//app.js
App({
  onLaunch: function () {
    // 读取本地存储的token
    var token = wx.getStorageSync('token') || null
    if(token){
      this.globalData.token = token,
      this.globalData.isLoginOk = true
    }

    //查看是否授权
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              console.log(this.globalData.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  post:function(url,data,cb){
    let header = {'Content-Type':'application/x-www-form-urlencoded'}
    if(this.globalData.token){
      header['Authorization']='JWT '+this.globalData.token
    }  
    wx.request({
      url: this.globalData.qiaofinn+url,
      data:this.getSendData(data),
      method:'post',
      header:header,
      complete:(res)=>{
        if(res.statusCode == 200 && res.data.status == true){
          return typeof cb == "function" && cb(res)
        }else{
          let result={
            err:res.statusCode,
            message:'网络错误',
          }
          if (typeof cb == "function") {
            cb(res)
          } else {
            this.showError(result)
          }
        }
      }
      
    })
  },
  isEmptyObject:function(obj) {
    for (var n in obj) {
      return false
    } 
    return true; 
  },
  get: function (url, data, cb,page_data) {
    // 设置分页
    if(!this.isEmptyObject(page_data)){
      if(url.charAt(url.length-1) === '/'){
        url += '?'
      }else{
        url += '&'
      }
      for (let key  in page_data) {
        url = url+key+'='+page_data[key]+'&'
      }
    } 
    let header = {}
    if (this.globalData.token) {
      header = { 'Authorization': 'JWT ' + this.globalData.token }
    }
    wx.request({
      url: this.globalData.qiaofinn + url,
      data:data,   // data: this.getSendData(data),
      method: 'get',
      header: header,
      complete: (res) => {
        if (typeof cb == "function") {
          return typeof cb == "function" && cb(res)
        } else {
          let result={
            err:res.statusCode,
            message:'网络错误',
          }
          this.showError(result)
        }
      }
    })
  },
  showError: function (err) {
    let title = '逗比用户，请先登录，好吗?'
    if (this.globalData.isLoginOk) {
      title = err.message
    } 

    if (this.globalData.isLoginOk) {
      wx.showToast({
        title: title,
        duration: 2500,
        icon: 'none'
      })
    } else {
      wx.showModal({
        title: '提示',
        content: title,
        showCancel: false
      })
    }    
  },
  getSendData:function(data){
    let sendData={
      // 'appid':this.globalData.appid,
      // 'openid':this.globalData.openid
    }
    for (let i  in data) {
      sendData[i] = data[i]
    }
    return sendData
  },
  globalData: {
    userInfo: null,
    qiaofinn:'http://127.0.0.1:8000',
    token:'',
    appid:'',
    openid:'',
    isLoginOk:false
  }
})